
import { BUTTONID, AREA, UISTATUS, FIELDS, CARDTABLEAREAIDS, DELETELINEBTNS } from '../../constance';
import { clearCardData } from '../utils/cardPageUtil';

function buttonControl(props) {
        let status = props.getUrlParam('status'); // 页面状态

        // 1、设置页面状态
        setUIState(props, status);
        // 2、按钮状态控制
        setButtonState(props, status);
        // 3、主按钮设置
        // setMainButton(props, status);
        // 4、返回按钮控件状态控制
        setHeadInfoState(props, status);
        // 5、卡片分页器状态控制
        setCardPaginationState(props, status);
}

function setUIState(props, status) {
        props.form.setFormStatus(AREA.cardFormId, status == UISTATUS.copy ? UISTATUS.add : status);
        let uistatus = status == UISTATUS.browse ? status : UISTATUS.edit;
        CARDTABLEAREAIDS.forEach((areaId) => {
                props.cardTable.setStatus(areaId, uistatus);
        });

        let isDisabled = status != UISTATUS.add;
        props.form.setFormItemsDisabled(AREA.cardFormId, { [FIELDS.pk_org]: isDisabled }); // 设置主组织编辑性
}

function setButtonState(props, status) {

        props.button.setButtonVisible(
                [
                        BUTTONID.Save,
                        BUTTONID.Cancel,
                        BUTTONID.cardBody_1_AddLine,
                        BUTTONID.cardBody_1_InsertLine,
                        BUTTONID.cardBody_2_AddLine,
                        BUTTONID.cardBody_2_InsertLine,
                        BUTTONID.cardBody_1_DeleteLine,
                        BUTTONID.cardBody_2_DeleteLine,
                        BUTTONID.cardBody_1_CopyLine,
                        BUTTONID.cardBody_2_CopyLine,
                        BUTTONID.cardBody_1_Upload,
                        BUTTONID.cardBody_1_DownTemplet,
                        BUTTONID.cardBody_2_Upload,
                        BUTTONID.cardBody_2_DownTemplet
                ],
                status != UISTATUS.browse
        );

        let btnShow = [
                BUTTONID.Add,
                BUTTONID.Edit,
                BUTTONID.Delete,
                BUTTONID.Copy,
                BUTTONID.Refresh,
                BUTTONID.Voucher,
                BUTTONID.RedHandle
        ];
        props.button.setButtonVisible(btnShow, status == UISTATUS.browse);

        let hasBlue =props.form.getFormItemsValue([ AREA.cardFormId ], 'isvoucher') && props.form.getFormItemsValue([ AREA.cardFormId ], 'isvoucher').value
        let hasRed =props.form.getFormItemsValue([ AREA.cardFormId ], 'isflag') && props.form.getFormItemsValue([ AREA.cardFormId ], 'isflag').value

        let btnNoShow = [];

        if(hasBlue){
                btnNoShow.push(BUTTONID.Edit, BUTTONID.Delete);
        }
        if(hasRed){
                btnNoShow.push(BUTTONID.Edit, BUTTONID.Delete, BUTTONID.Voucher, BUTTONID.RedHandle);
        }

        props.button.setButtonVisible(btnNoShow, false);


        props.button.setButtonVisible(
                [
                        BUTTONID.cardBody_1_PasteLineToTail,
                        BUTTONID.cardBody_2_PasteLineToTail,
                        BUTTONID.cardBody_1_CancelB,
                        BUTTONID.cardBody_2_CancelB

                ],
                false
        );

        if( status != UISTATUS.browse){
                props.button.setButtonVisible(
                        [
                                BUTTONID.Voucher,
                                BUTTONID.RedHandle
        
                        ],
                        false
                );
        }


        let hiddenBtn = [BUTTONID.cardBody_1_AddLine, BUTTONID.cardBody_1_InsertLine, BUTTONID.cardBody_1_CopyLine,BUTTONID.cardBody_2_AddLine, BUTTONID.cardBody_2_InsertLine, BUTTONID.cardBody_2_CopyLine];
        props.button.setButtonVisible(hiddenBtn, false);

        if(status == UISTATUS.browse){
                //浏览态
                
                props.button.setButtonVisible([BUTTONID.cardBody_1_Upload, BUTTONID.cardBody_1_DownTemplet, BUTTONID.cardBody_2_Upload, BUTTONID.cardBody_2_DownTemplet], false);
        }else {
                //编辑态
                let id = props.form.getFormItemsValue([ AREA.cardFormId ], 'pk_manualcost') && props.form.getFormItemsValue([ AREA.cardFormId ], 'pk_manualcost').value
                
                if(id == null){
                        props.button.setButtonVisible([BUTTONID.cardBody_1_Upload, BUTTONID.cardBody_1_DownTemplet, BUTTONID.cardBody_2_Upload, BUTTONID.cardBody_2_DownTemplet], true);
                }else{
                        props.button.setButtonVisible([BUTTONID.cardBody_1_Upload, BUTTONID.cardBody_1_DownTemplet, BUTTONID.cardBody_2_Upload, BUTTONID.cardBody_2_DownTemplet], false);
                }
                
        }


        


        // let approveStatus = props.form.getFormItemsValue([ AREA.cardFormId ], 'billstatus').value;

        // let isVoucher = props.form.getFormItemsValue([ AREA.cardFormId ], 'isvoucher').value;
         
        // if(isVoucher===true){
        //         props.button.setButtonVisible([BUTTONID.Edit,BUTTONID.Delete,BUTTONID.Voucher], false);
        //         props.button.setButtonVisible([BUTTONID.RedHandle], true);
        // }else{
        //         props.button.setButtonVisible([BUTTONID.Edit,BUTTONID.Delete,BUTTONID.Voucher], true);
        //         props.button.setButtonVisible([BUTTONID.RedHandle], false);

        // }
      
        // let isflag = props.form.getFormItemsValue([ AREA.cardFormId ], 'isflag').value;
        // if(isflag===true){
        //         props.button.setButtonVisible([BUTTONID.Edit,BUTTONID.Delete,BUTTONID.Voucher,BUTTONID.RedHandle], false);
        // }


        // if (approveStatus == '-1') {
        //         props.button.setButtonVisible([BUTTONID.Commit], false);
        //         //props.button.setButtonVisible([BUTTONID.Commit], true);
        //         props.button.setButtonVisible([BUTTONID.UnCommit], false);
        // } else if (approveStatus == '2' || approveStatus == '3') {
        //         props.button.setButtonVisible([BUTTONID.Commit, BUTTONID.Edit, BUTTONID.Delete, BUTTONID.Copy], false);
        //         props.button.setButtonVisible([BUTTONID.UnCommit], true);
        // } else {
        //         props.button.setButtonVisible([BUTTONID.Commit, BUTTONID.UnCommit, BUTTONID.Edit, BUTTONID.Copy, BUTTONID.Delete], false);
        // }

        // const scene = props.getUrlParam('scene');
        // if (scene === 'approvesce') {
        //         props.button.setButtonVisible([ BUTTONID.Edit, BUTTONID.Refresh ], status == UISTATUS.browse);
        //         props.button.setButtonVisible([ BUTTONID.Save, BUTTONID.Refresh ], status != UISTATUS.browse);
        //         props.button.setButtonVisible([BUTTONID.Add, BUTTONID.UnCommit, BUTTONID.Commit, BUTTONID.Delete, BUTTONID.Copy], false);
        // }
}

function setHeadInfoState(props, status) {
        props.BillHeadInfo.setBillHeadInfoVisible({
                showBackBtn: status == UISTATUS.browse,
                showBillCode: true,
                billCode: null
        });
        const scene = props.getUrlParam('scene');
        if (scene === 'approvesce') {
                props.BillHeadInfo.setBillHeadInfoVisible({
                        showBackBtn: false,
                        showBillCode: true,
                        billCode: null
                });
        }
}

function setCardPaginationState(props, status) {
        props.cardPagination.setCardPaginationVisible('cardPaginationBtn', status == UISTATUS.browse);
        const scene = props.getUrlParam('scene');
        if (scene === 'approvesce') {
                props.cardPagination.setCardPaginationVisible('cardPaginationBtn', false);
        }
}


/**
 * 设置卡片肩部按钮可用性：删行、复制行
 */
function onRowsSelected(props, moduleId) {
        let buttonArr = DELETELINEBTNS[moduleId];
        let checkedRows = props.cardTable.getCheckedRows(moduleId);
        if (checkedRows && checkedRows.length > 0) {
                props.button.setButtonDisabled(buttonArr, false);
        } else {
                props.button.setButtonDisabled(buttonArr, true);
        }
}

/**
 * 无数据时，设置卡片空界面
 * @param {*} props
 */
function setEmptyValueDisplay(props) {
        clearCardData(props); // 清空卡片数据
        setUIState(props, UISTATUS.browse); // 设置卡片编辑性

        props.BillHeadInfo.setBillHeadInfoVisible({
                showBackBtn: true,
                showBillCode: false
        });
        // 设置按钮可见性
        props.button.setButtonVisible([ BUTTONID.Add ], true);
        props.button.setButtonVisible(
                [
                        BUTTONID.Edit,
                        BUTTONID.Delete,
                        BUTTONID.Copy,
                        BUTTONID.Refresh,

                        BUTTONID.Save,
                        BUTTONID.Cancel,
                        BUTTONID.Voucher,
                        BUTTONID.RedHandle,

                        BUTTONID.cardBody_1_AddLine,
                        BUTTONID.cardBody_1_InsertLine,
                        BUTTONID.cardBody_2_AddLine,
                        BUTTONID.cardBody_2_InsertLine,
                        BUTTONID.cardBody_1_DeleteLine,
                        BUTTONID.cardBody_2_DeleteLine,
                        BUTTONID.cardBody_1_CopyLine,
                        BUTTONID.cardBody_2_CopyLine,
                        BUTTONID.cardBody_1_PasteLineToTail,
                        BUTTONID.cardBody_2_PasteLineToTail,
                        BUTTONID.cardBody_1_CancelB,
                        BUTTONID.cardBody_2_CancelB,
                        BUTTONID.cardBody_1_Upload,
                        BUTTONID.cardBody_2_Upload,
                        BUTTONID.cardBody_1_DownTemplet,
                        BUTTONID.cardBody_2_DownTemplet
                ],
                false
        );
        setCardPaginationState(props, false);
}

export { buttonControl, onRowsSelected, setEmptyValueDisplay};
